/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.ui;

import org.eclipse.jface.resource.CompositeImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;

/**
 * The Class AbstractCompositeImageDescriptor.
 */
public abstract class AbstractCompositeImageDescriptor extends
		CompositeImageDescriptor {

	/** The base image. */
	private Image baseImage;

	/** The flags. */
	private int flags;

	/** The size. */
	private Point size;

	/**
	 * Instantiates a new abstract composite image descriptor.
	 * 
	 * @param baseImage
	 *            the base image
	 * @param flags
	 *            the flags
	 */
	protected AbstractCompositeImageDescriptor(Image baseImage, int flags) {
		this.baseImage = baseImage;
		this.flags = flags;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof AbstractCompositeImageDescriptor)) {
			return false;
		}
		AbstractCompositeImageDescriptor other = (AbstractCompositeImageDescriptor) object;
		return (baseImage.equals(other.baseImage) && flags == other.flags);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return baseImage.hashCode() | flags;
	}

	/**
	 * Gets the flags.
	 * 
	 * @return the flags
	 */
	public int getFlags() {
		return flags;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.resource.CompositeImageDescriptor#getSize()
	 */
	@Override
	protected final Point getSize() {
		if (size == null) {
			ImageData data = baseImage.getImageData();
			size = new Point(data.width, data.height);
		}
		return size;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage
	 * (int, int)
	 */
	@Override
	protected final void drawCompositeImage(int width, int height) {
		ImageData background = baseImage.getImageData();
		if (background == null) {
			background = DEFAULT_IMAGE_DATA;
		}
		drawImage(background, 0, 0);
		drawOverlays();
	}

	/**
	 * Draw overlays.
	 */
	protected abstract void drawOverlays();
}
